<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { queryAppointmentJoinTime } from '@/api/data/reserve'
const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      queryParamsJoinTime: {
        joinTime: null
      },
      arrayDate: [],
      joinCount: []
    }
  },
  created(){
          this.getPastSevenDays()
      this.getArrayDate(this.arrayDate)
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  watch: {
    joinCount(newValue) {
      this.joinCount = newValue
       this.chart.setOption({ series: [
          {
            data: this.joinCount,
          }
        ]})
    }
  },
  methods: {
async  getArrayDate (date) {
  let dateArrays = date;
  for (const date of dateArrays) {
      this.queryParamsJoinTime.joinTime = date
    const contents = await queryAppointmentJoinTime(this.queryParamsJoinTime);
     this.joinCount.push(contents.data.length)
  }
},
    //获取日期
    getPastSevenDays() {
      const result = []
      const today = new Date()
      today.setDate(today.getDate() + 1)
      // 从今天开始，向前推算七天
      for (let i = 0; i < 7; i++) {
        today.setDate(today.getDate() - 1) // 减去一天
        // result.unshift(new Date(today.getFullYear(), today.getMonth(), today.getDate())); // 将日期添加到数组的开始
        const formattedDate = today.getFullYear() + '-' + (today.getMonth() + 1).toString().padStart(2, '0') + '-' + today.getDate().toString().padStart(2, '0')
        result.push(formattedDate)
        this.arrayDate.push(formattedDate)
      }

      return result
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
   
      this.chart.setOption({
        title: {
          text: '近七日入住情况',
          left: 'center',
          textStyle: {
            fontSize: 18 // 设置标题字体大小为 16px
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: 50,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: this.arrayDate,
            axisTick: {
              alignWithLabel: true
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            axisTick: {
              show: false
            }
          }
        ],
        series: [
          {
            name: '入住人数',
            type: 'bar',
            stack: 'vistors',
            barWidth: '60%',
            data: this.joinCount,
            animationDuration
          }
        ]
      })
    }
  }
}
</script>
